﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XichLoGreen.Models;
using PagedList;

namespace XichLoGreen.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        GreenIntities db = new GreenIntities();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Search(string searchkey)
        {
            Session["Key"] = searchkey;
            return RedirectToAction("SearchResult");
        }
        private static readonly string[] VietNamChar = new string[] 
        { 
            "aAeEoOuUiIdDyY", 
            "áàạảãâấầậẩẫăắằặẳẵ", 
            "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ", 
            "éèẹẻẽêếềệểễ", 
            "ÉÈẸẺẼÊẾỀỆỂỄ", 
            "óòọỏõôốồộổỗơớờợởỡ", 
            "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ", 
            "úùụủũưứừựửữ", 
            "ÚÙỤỦŨƯỨỪỰỬỮ", 
            "íìịỉĩ", 
            "ÍÌỊỈĨ", 
            "đ", 
            "Đ", 
            "ýỳỵỷỹ", 
            "ÝỲỴỶỸ" 
        };
        public static string LocDau(string str)
        {   
            for (int i = 1; i < VietNamChar.Length; i++)
            {
                for (int j = 0; j < VietNamChar[i].Length; j++)
                    str = str.Replace(VietNamChar[i][j], VietNamChar[0][i - 1]);
            }
            return str;
        }
        public ActionResult SearchResult(int? page, string searchkey)
        {
            // var lstNews = db.News.ToList();
            try
            {
                if (searchkey != null)
                    Session["Key"] = searchkey;
                string key = Session["Key"].ToString();
                if (key == null || key.Equals(string.Empty))
                {
                    return View();
                }
                List<NewsModel> lstNews = db.News.ToList();
                List<NewsModel> list = new List<NewsModel>();
                foreach (NewsModel item in lstNews)
                {
                    key = key.Trim();
                    int find = 0;
                    string temp;
                    string tempkey=key.ToLower();
                    tempkey = LocDau(tempkey);
                    temp = item.Title.ToLower();
                    temp = LocDau(temp);
                    int vitri = temp.IndexOf(tempkey);
                    if (vitri != -1)
                    {
                        find = 1;
                        //string a = item.Title.Insert(tempkey.Length, "</strong>");
                        //a=a.Insert(vitri, "<strong>");
                       // item.Title = a;
                    }
                    temp = item.Content.ToLower();
                    temp = LocDau(temp);
                    vitri = temp.IndexOf(tempkey);
                    if (vitri != -1)
                        find = 1;
                    string[] array = tempkey.Split(' ');
                    for (int i = 0; i < array.Length; i++)
                    {
                        temp=item.Title;
                        vitri = temp.IndexOf(array[i]);
                        if (vitri != -1)
                            find = 1;
                        temp = item.Content;
                        vitri = temp.IndexOf(array[i]);
                        if (vitri != -1)
                            find = 1;
                    }
                    if (find == 1)
                        list.Add(item);
                }
                Session["SL"] = list.Count();
                int pageSize = 5;
                int pageNumber = (page ?? 1);
                return View(list.ToPagedList(pageNumber, pageSize));
            }
            catch (Exception ex)
            {
                return View(); 
            }
        }

        public ActionResult Logout()
        {
            Session["Account"] = null;
            return RedirectToAction("Index");
        }
    }
}
